//
//  MainTitleToolView.swift
//  SwiftMusicPlayer
//
//  Created by 顾佳槟 on 2018/3/7.
//  Copyright © 2018年 lancoo. All rights reserved.
//

import UIKit
typealias ButtonDidClickBlock = (Int) -> Void

class MainTitleToolView: BaseView {
    
    var owerBUtton:UIButton?
    var recommendButton:UIButton?
    var musicButton:UIButton?
    var lineLabel:UILabel?
    var buttonDidClickBlock:ButtonDidClickBlock?
    
    override func setupSubviews() {
        
        self.owerBUtton = UIButton.init(frame: CGRect.init(x: 10, y: 7, width: 40, height: 30))
        owerBUtton?.setTitle("我", for: .normal)
        owerBUtton?.tag = 1
        owerBUtton?.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        self.addSubview(owerBUtton!)

        
        self.recommendButton = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 40, height: 30))
        self.recommendButton?.center = self.center
        recommendButton?.tag = 2
        recommendButton?.setTitle("推荐", for: .normal)
        recommendButton?.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        self.addSubview(recommendButton!)

        self.musicButton = UIButton.init(frame: CGRect.init(x: self.frame.size.width - 50, y: 7, width: 40, height: 30))
        musicButton?.setTitle("乐馆", for: .normal)
        musicButton?.tag = 3
        musicButton?.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        self.addSubview(musicButton!)

        self.lineLabel = UILabel.init(frame: CGRect.init(x:0, y:  self.frame.size.height - 2, width: 28, height: 2))
        self.lineLabel?.center.x = (owerBUtton?.center.x)!
        lineLabel?.backgroundColor = UIColor.init(hexString: "F4506C")
        self.addSubview(lineLabel!)

        resetButtonTitleColor()
        owerBUtton?.setTitleColor(UIColor.init(hexString: "F4506C"), for: .normal)
    }
    
    override func config() {
        self.backgroundColor = UIColor.clear
    }
}

//按钮点击方法
extension MainTitleToolView{
    
    @objc func buttonClick(sender:UIButton){
        refresh(sender: sender)
        if buttonDidClickBlock != nil {
            buttonDidClickBlock!(sender.tag)
        }
    }
    
    func refresh(sender:UIButton) {
        resetButtonTitleColor()
        sender.setTitleColor(UIColor.init(hexString: "F4506C"), for: .normal)
        moveLineLabel(centerXOffset: sender.center.x - (self.lineLabel?.center.x)! ,animated: true)
    }
    
    func switchToButton(index:NSInteger){
        let button:UIButton?
        switch index {
        case 1:
            button = owerBUtton
        case 2:
            button = recommendButton
        case 3:
            button = musicButton
        default:
            button = owerBUtton
        }
        
        refresh(sender: button!)
    }
    
    func moveLineLabel(centerXOffset:CGFloat,animated:Bool){
        
        if animated {
            UIView.animate(withDuration: 0.2) {
                self.lineLabel?.center.x = (self.lineLabel?.center.x)! + centerXOffset
            }
        }
        else{
            resetButtonTitleColor()
            self.lineLabel?.center.x = (self.lineLabel?.center.x)! + centerXOffset
        }
    }
    
    func resetButtonTitleColor(){
        owerBUtton?.setTitleColor(UIColor.black, for: .normal)
        recommendButton?.setTitleColor(UIColor.black, for: .normal)
        musicButton?.setTitleColor(UIColor.black, for: .normal)
    }
}
